<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 充值管理
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;
use app\backend\validate\RechargeValidate;
use app\backend\validate\WithdrawValidate;
use think\facade\Db;

class Recharge extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if(request()->isPost()){
            $validate = new WithdrawValidate();
            $ret = $validate->scene('index')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $page = input('page/d',1);
            $limit = input('limit/d',20);
            $status = input('status','');
            $fieldName = input('field_name','');
            $fieldValue = input('field_value','');
            $dateType = input('date_type','created_at');
            $startDate = input('start_date','');
            $endDate = input('end_date','');

            $where = [
                [$dateType,'between',[$startDate,$endDate]]
            ];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)){
                switch($fieldName){
                    case 'account'://手机
                        $userId = \app\extend\model\User::where([
                            ['account','=',$fieldValue]
                        ])->value('id');
                        $where[] = ['uid','=',$userId];
                        break;
                    case 'uid'://用户ID
                        $where[] = ['uid', '=', $fieldValue];
                        break;
                }
            }

            //审核状态
            if (is_numeric($status)) {
                $where[] = ['status', '=', $status];
            }

            $list = [];
            $count = \app\extend\model\Recharge::where($where)->count();
            if($count > 0){
                $list = \app\extend\model\Recharge::where($where)->append(['user_account_txt'])->order(['id' => 'desc'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('index');
        }
    }

    /**
     * 审核
     */
    public function check()
    {
        if(request()->isPost()){
            $validate = new RechargeValidate();
            $ret = $validate->scene('check')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $id = input('id/d',0);
            $status = input('status/d',0);

            $rechargeInfo = \app\extend\model\Recharge::where([
                ['id','=',$id]
            ])->field('id,status,uid,money')->find();
            if(empty($rechargeInfo)){
                return json(_E('数据不存在'));
            }

            if($rechargeInfo->status != 0){
                return json(_E('该记录已经审核过'));
            }

            $userInfo = \app\extend\model\User::where([
                ['id','=',$rechargeInfo->uid],
            ])->find();

            try {
                // 启动事务
                Db::startTrans();

                \app\extend\model\Recharge::where([
                    ['id','=',$id],
                    ['status','=',0]
                ])->update([
                    'balance' => $userInfo->balance + $rechargeInfo->money,
                    'operater' => session('nickname'),
                    'status' => $status,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                if($status == 1){//通弄需要加金额
                    \app\extend\model\User::where([
                        ['id','=',$userInfo->id],
                    ])->update([
                        'balance' => $userInfo->balance + $rechargeInfo->money
                    ]);
                }

                Db::commit();
                return json(_S('审核成功'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(_E('审核失败,错误:' . $e->getMessage()));
            }
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        if (request()->isPost()) {
            $validate = new RechargeValidate();
            $ret = $validate->scene('del')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }
            $id = input('id/d', 0);

            $result = \app\extend\model\Recharge::where([
                ['id', '=', $id]
            ])->delete();
            if (!$result) {
                return json(_E('删除失败'));
            } else {
                return json(_S('删除成功'));
            }
        }
    }
}
